﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using G15_Agent.Models;
using System.Data;

namespace G15_Agent.Controllers
{
     
    public class DanhSachNCCController : Controller
    {
         private AgentContext DatabaseSQL = new AgentContext();
        //
        // GET: /DanhSachNCC/
        
        public ActionResult Index()
        {
             List<DanhSachNCC> ListNCC = DatabaseSQL.DanhSachNCCs.ToList();
                return View(ListNCC);
        }

        public ActionResult Create()
        {
            ViewBag.LoaiNCC = new SelectList(DatabaseSQL.LoaiNCCs, "MaLoai", "TenLoai");
            return View();
        }

        [HttpPost]
        public ActionResult Create(DanhSachNCC model)
        {
            if (ModelState.IsValid)
            {
                DanhSachNCC NCC = CreateNewNCC(model);
                DatabaseSQL.DanhSachNCCs.Add(NCC);
                DatabaseSQL.SaveChanges();

                TempData["message"] = string.Format("{0} Đã được tạo", model.TenNCC);
                return RedirectToAction("Index");
            }
            ViewBag.LoaiNCC = new SelectList(DatabaseSQL.LoaiNCCs, "MaLoai", "TenLoai", model.MaNCC);
            return View(model);
        }

        private DanhSachNCC CreateNewNCC(DanhSachNCC model)
        {
            DanhSachNCC NCC = new DanhSachNCC();
            List<DanhSachNCC> ListNCC = DatabaseSQL.DanhSachNCCs.ToList();

            NCC.TenNCC = model.TenNCC;
            NCC.TLGiaNCC = model.TLGiaNCC;
            NCC.TLGiaKH = model.TLGiaKH;
            NCC.DiaChiWS = model.DiaChiWS;
            NCC.Username = model.Username;
            NCC.Pass = model.Pass;
            NCC.LoaiNCC = model.LoaiNCC;
            NCC.TrangThai = true;
            if (model.LoaiNCC == 1)
            {
                List<DanhSachNCC> ListAI = DatabaseSQL.DanhSachNCCs.Where(d => d.MaNCC.StartsWith("AI")).ToList();
                NCC.MaNCC = "AI" + (ListAI.Count + 1).ToString("000000000");
            }
            if (model.LoaiNCC == 2)
            {
                List<DanhSachNCC> ListHotel = DatabaseSQL.DanhSachNCCs.Where(d => d.MaNCC.StartsWith("H")).ToList();
                NCC.MaNCC = "H" + (ListHotel.Count + 1).ToString("000000000");
            }
            if (model.LoaiNCC == 3)
            {
                List<DanhSachNCC> ListTS = DatabaseSQL.DanhSachNCCs.Where(d => d.MaNCC.StartsWith("TS")).ToList();
                NCC.MaNCC = "TS" + (ListTS.Count + 1).ToString("000000000");
            }
            if (model.LoaiNCC == 4)
            {
                List<DanhSachNCC> ListTV = DatabaseSQL.DanhSachNCCs.Where(d => d.MaNCC.StartsWith("TV")).ToList();
                NCC.MaNCC = "TV" + (ListTV.Count + 1).ToString("000000000");
            }


            return NCC;
        }

        public ActionResult Edit(string id)
        {
            DanhSachNCC model = DatabaseSQL.DanhSachNCCs.Find(id);
            ViewBag.LoaiNCC = new SelectList(DatabaseSQL.LoaiNCCs, "MaLoai", "TenLoai");
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(DanhSachNCC model)
        {
            if (ModelState.IsValid)
            {
                DatabaseSQL.Entry(model).State = EntityState.Modified;
                DatabaseSQL.SaveChanges();
                TempData["message"] = string.Format("{0} Đã được lưu", model.TenNCC);
                return RedirectToAction("Index");
            }
            ViewBag.LoaiNCC = new SelectList(DatabaseSQL.LoaiNCCs, "MaLoai", "TenLoai");
            return View(model);
        }

        public ActionResult Delete(string id)
        {
            DanhSachNCC model = DatabaseSQL.DanhSachNCCs.Find(id);
            
            return View(model);
        }

        //
        

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {
            DanhSachNCC model = DatabaseSQL.DanhSachNCCs.Find(id);
            DatabaseSQL.DanhSachNCCs.Remove(model);
            DatabaseSQL.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult Active(string id)
        {
            DanhSachNCC model = DatabaseSQL.DanhSachNCCs.Find(id);

            return View(model);
        }

        //
        

        [HttpPost, ActionName("Active")]
        public ActionResult KichHoat(string id)
        {
            DanhSachNCC model = DatabaseSQL.DanhSachNCCs.Find(id);
            model.TrangThai = true;
            DatabaseSQL.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult Inactive(string id)
        {
            DanhSachNCC model = DatabaseSQL.DanhSachNCCs.Find(id);

            return View(model);
        }

        //
       

        [HttpPost, ActionName("Inactive")]
        public ActionResult VoHieu(string id)
        {
            DanhSachNCC model = DatabaseSQL.DanhSachNCCs.Find(id);
            model.TrangThai = false;
            DatabaseSQL.SaveChanges();
            return RedirectToAction("Index");
        }
    }
}
